package com.lekang.service.impl;

import java.util.List;
import com.lekang.common.utils.DateUtils;
import com.lekang.dto.CheckInManageDto;
import com.lekang.service.ITaskGenerationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lekang.mapper.CheckInManagementMapper;
import com.lekang.domain.CheckInManagement;
import com.lekang.service.ICheckInManagementService;

/**
 * 入住管理Service业务层处理
 * 
 * @author chengbingzhen
 * @date 2025-10-15
 */
@Service
public class CheckInManagementServiceImpl implements ICheckInManagementService 
{
    private static final Logger log = LoggerFactory.getLogger(CheckInManagementServiceImpl.class);
    
    @Autowired
    private CheckInManagementMapper checkInManagementMapper;
    
    @Autowired
    private ITaskGenerationService taskGenerationService;

    /**
     * 查询入住管理
     * 
     * @param id 入住管理主键
     * @return 入住管理
     */
    @Override
    public CheckInManagement selectCheckInManagementById(Long id)
    {
        return checkInManagementMapper.selectCheckInManagementById(id);
    }

    @Override
    public CheckInManageDto selectCheckInManagementByIdDto(Long id) {
        return checkInManagementMapper.selectCheckInManagementByIdDto(id);
    }

    @Override
    public CheckInManagement selectCheckInManagementByElder(Long id) {
        return checkInManagementMapper.selectCheckInManagementByElder(id);
    }

    /**
     * 查询入住管理列表
     * 
     * @param checkInManagement 入住管理
     * @return 入住管理
     */
    @Override
    public List<CheckInManagement> selectCheckInManagementList(CheckInManagement checkInManagement)
    {
        return checkInManagementMapper.selectCheckInManagementList(checkInManagement);
    }

    /**
     * 新增入住管理
     * 
     * @param checkInManagement 入住管理
     * @return 结果
     */
    @Override
    public int insertCheckInManagement(CheckInManagement checkInManagement)
    {
        checkInManagement.setCreateTime(DateUtils.getNowDate());
        return checkInManagementMapper.insertCheckInManagement(checkInManagement);
    }

    /**
     * 修改入住管理
     * 
     * @param checkInManagement 入住管理
     * @return 结果
     */
    @Override
    public int updateCheckInManagement(CheckInManagement checkInManagement)
    {
        return checkInManagementMapper.updateCheckInManagement(checkInManagement);
    }

    /**
     * 批量删除入住管理
     * 
     * @param ids 需要删除的入住管理主键
     * @return 结果
     */
    @Override
    public int deleteCheckInManagementByIds(Long[] ids)
    {
        return checkInManagementMapper.deleteCheckInManagementByIds(ids);
    }

    /**
     * 删除入住管理信息
     * 
     * @param id 入住管理主键
     * @return 结果
     */
    @Override
    public int deleteCheckInManagementById(Long id)
    {
        return checkInManagementMapper.deleteCheckInManagementById(id);
    }

    @Override
    public int deleteCheckInManagementByElderlyId(Long id) {
        return checkInManagementMapper.deleteCheckInManagementByElderlyId(id);
    }
    
    /**
     * 入住完成后的业务处理（生成账单 + 生成任务）
     * 
     * 说明：
     * 1. 此方法应在入住审批通过、账单生成完成后调用
     * 2. 会自动为老人生成首月的护理任务
     * 3. 任务生成失败会回滚事务
     * 
     * @param checkInId 入住管理ID
     * @return 生成的任务数量
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int completeCheckIn(Long checkInId) 
    {
        try {
            log.info("开始处理入住完成业务，入住ID: {}", checkInId);
            
            // 1. 查询入住信息
            CheckInManagement checkIn = checkInManagementMapper.selectCheckInManagementById(checkInId);
            if (checkIn == null) {
                log.warn("入住ID: {} 不存在", checkInId);
                return 0;
            }
            
            // 2. 生成首月账单（这里假设已经生成，实际项目中可能需要调用账单服务）
            // TODO: 调用账单生成服务
            // billService.generateFirstMonthBill(checkInId);
            
            // 3. 生成首月护理任务
            int taskCount = taskGenerationService.generateFirstMonthTasks(checkInId);
            log.info("入住ID: {} 首月任务生成成功，共生成 {} 个任务", checkInId, taskCount);
            
            return taskCount;
        } catch (Exception e) {
            log.error("入住ID: {} 完成业务处理失败", checkInId, e);
            throw e;
        }
    }
}
